package com.jmlee.controller;

import com.jmlee.pojo.PageBean;
import com.jmlee.pojo.Users;
import com.jmlee.service.FrontUserService;
import com.jmlee.util.JSONUtil;
import com.jmlee.util.SecurityUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 前台用户管理
 *
 * @author xl W
 * @date 2021/3/13 15:56
 */

@Controller
public class FrontUserController {


    @Value("${default.password}")
    private String pwd;

    @Autowired
    private FrontUserService frontUserService;

    //========================前台用户表===============================

    /**
     * 前台用户
     *
     * @author xl W
     * @date 2021/3/6 23:45
     */
    //跳转前台用户页面
    @RequestMapping("turnUser")
    public String turnUser() {
//        System.out.println("来过了");
        return "userManage";
    }

    //用户总数据显示表格
    @RequestMapping("usersCount")
    @ResponseBody
    public String count(Users users, HttpServletRequest request, HttpServletResponse response) throws IOException {

        int curPage = Integer.valueOf(request.getParameter("page"));
        int pageSize = Integer.valueOf(request.getParameter("limit"));

//        System.out.println(users.toString());

        RowBounds rowBounds = new RowBounds((curPage - 1) * pageSize, pageSize);
        PageBean<Users> pageBean = new PageBean<>();

        pageBean.setCode(0);
        int count = frontUserService.selectUsersListCount(users);

        pageBean.setCount(count);
        pageBean.setData(frontUserService.selectUsersList(users, rowBounds));


        String json = JSONUtil.objectToJson(pageBean);
//        System.out.println(json);
        return json;
    }

    //前台用户禁用
    @RequestMapping("userForbidden")
    @ResponseBody
    public String userForbidden(@RequestBody Users users, HttpServletRequest request) throws IOException {
//        System.out.println("嗯" + users.toString());
        boolean s = frontUserService.usersForbidden(users);
        if (s == true) {
            return "禁用成功";
        }

        return "禁用失败";
    }

    //前台用户启用
    @RequestMapping("userStart")
    @ResponseBody
    public String userStart(@RequestBody Users users, HttpServletRequest request) throws IOException {
//        System.out.println("嗯" + users.toString());

        boolean s = frontUserService.usersStart(users);
        if (s == true) {
            return "启用成功";
        }
        return "启用失败";
    }


    @RequestMapping("/changePwd")
    @ResponseBody
    public String changePwd(@RequestBody Users users, HttpServletRequest request) throws IOException {

        String password = SecurityUtil.getMD5(pwd);
        users.setPassword(password);

        boolean s = frontUserService.userChangePwd(users);
        if (s == true) {
            return "修改成功";
        }

        return "修改失败";
    }

}
